package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService{
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission select(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }
        @Autowired
        private ApplicationContext ctx; //spring 上下文
    @Override
    public void reload() {

        //获取数据库中的所有权限
        List<String> permissions = permissionMapper.selectAllExpression();

        //1 获取所有controller
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = map.values();
        //遍历每个controller 获取每个方法
        for (Object controller : controllers) {
            //如果不是cglib的代理对象,就不往下执行
            if(AopUtils.isCglibProxy(controller)){
                continue;
            }
            /*//因为需要获取的方法 在controller里没有 所以要用 controller 的字节码对象 通过反射去获取需要的方法*/
            //controller如果贴了shiro注解,shiro就会对该controller进行cglib(继承)代理
            //现在获取到的是代理对象,无法获取父类的注解,所以要先拿父类,再从父类中获取注解
            Class clazz = controller.getClass().getSuperclass();
            Method[] declaredMethods = clazz.getDeclaredMethods();//当前字节码中的方法,不要父类中的方法 所以用getDeclaredMethods
            for (Method method : declaredMethods) {
                //获取方法上的注解
                //遍历每个方法,判断是否有 RequiredPermission 注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                // 没有 -->跳过下边步骤
                //有  -->
                if (annotation!=null){
                    //获取注解上的权限名称,权限表达式
                    //方式一 直接从注解中获取
                    String expression = annotation.value()[0];
                    //方式二 反射获取
                    String simpleNmae = clazz.getSimpleName().replace("Controller", "");
                    //uncapitaliza方法可以吧某个字符串的首字母变为小写
                    //String s = StringUtils.uncapitalize(simpleNmae) + ":" + method.getName();
                    //判断权限表达式是否存在数据库中
                    if (!permissions.contains(expression)){
                        String name = annotation.value()[1];
                        //封装为权限对象
                        Permission permission = new Permission();
                        permission.setExpression(expression);
                        permission.setName(name);
                        //把对象插入数据库中
                        permissionMapper.insert(permission);
                    }
                }
            }
        }

    }

    @Override
    public List<String> selectExpressionByEmpId(Long emloyeeId) {
        return permissionMapper.selectExpressionByEmpId(emloyeeId);
    }

}
